import json

# 读取原始的 COCO 数据集文件
input_file = 'test.json' 
output_file = 'test_after.json' 

with open(input_file, 'r') as file:
    data = json.load(file)

# 遍历所有的 annotations
for annotation in data['annotations']:
    if 'segmentation' in annotation:
        new_segmentation = []
        # 遍历每一个 segmentation
        for point in annotation['segmentation']:
            # 直接将点对添加到新的列表中
            new_segmentation.extend(point) 
        # 更新annotation中的segmentation字段
        annotation['segmentation'] = [new_segmentation] 

# 保存修正后的数据到新的 JSON 文件
with open(output_file, 'w') as file:
    json.dump(data, file, indent=4)

print("修正完成，修正后的数据已保存至:", output_file)